<?
require_once('config.php');
require_once('Idioma.php');

//Funcion que comprueba si el usuario esta loggeado, debe utilizarse antes de cualquier salida.
function CheckSession(){
	if(empty($_SESSION))
		session_start();

	global $UserID, $ColID;
	
	$UserID = $_SESSION['s_UserID'];
	$ColID = $_SESSION['s_ColID'];
}

function ConectObj(){
	global $db_data;
	$mysqli = new MySQLi($db_data->host,$db_data->user,$db_data->password,$db_data->database);
	if($mysqli->connect_error)
		die('Error al conectar a la base de datos: '.$mysqli->connect_error);
	return $mysqli;
}

function getSpells($raze=NULL, $pers=NULL){
	$mysqli = ConectObj();
	$query = 'SELECT * FROM spells WHERE Activo = "1" ORDER BY ID';
	if($result = $mysqli->query($query)){
		while($data = $result->fetch_assoc())
			$Spells[$data[ID]] = $data;
		return $Spells;
	}
}

function checkSpell($spell,$spells=0){
	if(!json_decode($spells)){
		return 0;
	}
	foreach(json_decode($spells) as $data){
		if($data[0] == $spell){
			return $data[1];
			break;
		}
	}
}

class user{
	function conect(){
		global $db_data;
		$mysqli = new MySQLi($db_data->host,$db_data->user,$db_data->password,$db_data->database);
				
		/* check connection */
		if (mysqli_connect_errno()){
			printf("Connect failed: %s\n", mysqli_connect_error());
			exit();
		}
		else
			return $mysqli;
	}
}

class Colony{
	public $ID, $UserID, $Colonia, $ColX, $ColY, $Lider, $Civil, $Pers;
	public $Tierra, $Aldeanos, $Oro, $Comida, $Piedra, $Runas;
	public $Explorando, $Casas, $Granjas ,$Comercios, $Minas, $Barracas, $Fortificaciones, $Templos, $Escuelas, $Mazmorras, $Establos;
	public $ProCasas, $ProGranjas, $ProComercios ,$ProMinas, $ProBarracas, $ProFortificaciones, $ProTemplos, $ProEscuelas, $ProMazmorras, $ProEstablos;
	public $Soldados, $Tropa1, $Tropa2, $Tropa3, $Elites, $TropaPers, $Caballos, $Chamanes, $Ladrones;
	public $EntTropa1, $EntTropa2, $EntTropa3, $EntElites, $EntTropaPers, $VelRec, $PorEjer;
	public $TemplosPri, $Spells, $Entereza;
	public $TropasFuera;
	
	function __construct($col){
		if($col*1){
			$WHERE = 'WHERE ID = '.$col;
		}else{
			$WHERE = 'WHERE Colonia = "'.$col.'"';
		}
		
		global $db_data;
		$mysqli = new MySQLi($db_data->host,$db_data->user,$db_data->password,$db_data->database);
		if($result = $mysqli->query('SELECT * FROM cols '.$WHERE)){
			$data = $result->fetch_object();
			$this->ID = $data->ID;
			$this->UserID = $data->UserID;
			$this->Colonia = $data->Colonia;
			$this->ColX = $data->ColX;
			$this->ColY = $data->ColY;
			$this->Lider = $data->Lider;
			$this->Civil = $data->Civil;
			$this->Pers = $data->Pers;
			$this->Tierra = $data->Tierra;
			$this->Aldeanos = $data->Aldeanos;
			$this->Oro = $data->Oro;
			$this->Comida = $data->Comida;
			$this->Piedra = $data->Piedra;
			$this->Runas = $data->Runas;
			$this->Explorando = $data->Explorando;
			$this->Casas = $data->Casas;
			$this->Granjas = $data->Granjas;
			$this->Comercios = $data->Comercios;
			$this->Minas = $data->Minas;
			$this->Barracas = $data->Barracas;
			$this->Fortificaciones = $data->Fortificaciones;
			$this->Templos = $data->Templos;
			$this->Escuelas = $data->Escuelas;
			$this->Mazmorras = $data->Mazmorras;
			$this->Establos = $data->Establos;
			$this->ProCasas = $data->ProCasas;
			$this->ProGranjas = $data->ProGranjas;
			$this->ProComercios = $data->ProComercios;
			$this->ProMinas = $data->ProMinas;
			$this->ProBarracas = $data->ProBarracas;
			$this->ProFortificaciones = $data->ProFortificaciones;
			$this->ProTemplos = $data->ProTemplos;
			$this->ProEscuelas = $data->ProEscuelas;
			$this->ProMazmorras = $data->ProMazmorras;
			$this->ProEstablos = $data->ProEstablos;
			$this->Soldados = $data->Soldados;
			$this->Tropa1 = $data->Tropa1;
			$this->Tropa2 = $data->Tropa2;
			$this->Tropa3 = $data->Tropa3;
			$this->Elites = $data->Elites;
			$this->TropaPers = $data->TropaPers;
			$this->Caballos = $data->Caballos;
			$this->Chamanes = $data->Chamanes;
			$this->Ladrones = $data->Ladrones;
			$this->EntTropa1 = $data->EntTropa1;
			$this->EntTropa2 = $data->EntTropa2;
			$this->EntTropa3 = $data->EntTropa3;
			$this->EntElites = $data->EntElites;
			$this->EntTropaPers = $data->EntTropaPers;
			$this->VelRec = $data->VelRec;
			$this->PorEjer = $data->PorEjer;
			$this->TemplosPri = $data->TemplosPri;
			$this->Spells = $data->Spells;
			$this->Entereza = $data->Entereza;
			$this->TropasFuera = $data->TropasFuera;
			unset($result,$data);
		}
		$mysqli->close();
	}
	
	function hasSpell($ID){ //Returns Spell duration [INT]
		foreach(json_decode($this->Spells) as $Spell){
			if($Spell[0] == $ID){
				return $Spell[1];
			}
		}
		return FALSE;
	}
	
	function addSpell($ID,$duration){ //Returns Spells [JSON]
		if(empty($this->Spells)){
			$Spells = array();
			array_push($Spells,array($ID,$duration));
		}
		else{
			$Spells = json_decode($this->Spells);
			array_push($Spells,array($ID,$duration));
		}
		$this->Spells = json_encode($Spells);
		return $this->Spells;
	}
	
	function removeSpell($ID){ //Returns Spell duration [INT]
		$Spells = json_decode($this->Spells);
		for($i=0;$i<count($Spells);$i++){
			if($Spells[$i][0] == $ID){
				$duration = $Spells[$i][1];
				array_splice($Spells,$i,1);
				$this->Spells = json_encode($Spells);
				return $duration;
			}
		}
		return FALSE;
	}
	
	function fixBuildings(){
		$totalBuildings = $this->Casas + $this->Granjas + $this->Comercios + $this->Minas + $this->Barracas + $this->Fortificaciones + $this->Templos + $this->Escuelas + $this->Mazmorras + $this->Establos + $this->ProCasas + $this->ProGranjas + $this->ProComercios + $this->ProMinas + $this->ProBarracas + $this->ProFortificaciones + $this->ProTemplos + $this->ProEscuelas + $this->ProMazmorras + $this->ProEstablos;
		
		if($totalBuildings > $this->Tierra){
			$excess = 1-($this->Tierra / $totalBuildings);
			$this->Casas = floor($this->Casas*(1-$excess));
			$this->Granjas = floor($this->Granjas*(1-$excess));
			$this->Comercios = floor($this->Comercios*(1-$excess));
			$this->Minas = floor($this->Minas*(1-$excess));
			$this->Barracas =floor( $this->Barracas*(1-$excess));
			$this->Fortificaciones = floor($this->Fortificaciones*(1-$excess));
			$this->Templos = floor($this->Templos*(1-$excess));
			$this->Escuelas = floor($this->Escuelas*(1-$excess));
			$this->Mazmorras = floor($this->Mazmorras*(1-$excess));
			$this->Establos = floor($this->Establos*(1-$excess));
		}
	}
	
	function save(){
		global $db_data;
		$mysqli = new MySQLi($db_data->host,$db_data->user,$db_data->password,$db_data->database);
		$query = 'UPDATE cols SET Tierra='.$this->Tierra.', Casas='.$this->Casas.', Granjas='.$this->Granjas.', Comercios='.$this->Comercios.', Minas='.$this->Minas.', Barracas='.$this->Barracas.', Fortificaciones='.$this->Fortificaciones.', Templos='.$this->Templos.', Escuelas='.$this->Escuelas.', Mazmorras='.$this->Mazmorras.', Establos='.$this->Establos.', Spells="'.$this->Spells.'" WHERE ID='.$this->ID;
		if($result = $mysqli->query($query))
			return TRUE;
		else
			return TRUE;
		
		$mysqli->close();
	}
	
	
}

class Spells{
	function __construct(){
		global $db_data;
		$mysqli = new MySQLi($db_data->host,$db_data->user,$db_data->password,$db_data->database);
	}
}